package com.sxfq.loansupermarket.backsystem.modular.weixin.controller;


import java.io.File;
import java.util.HashMap;
import java.util.Map;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.sxfq.loansupermarket.backsystem.modular.weixin.config.DownLoadImg;
import com.sxfq.loansupermarket.backsystem.modular.weixin.config.GetBody;
import com.sxfq.loansupermarket.backsystem.modular.weixin.config.OssUtil2;

@Controller
public class TestUrl {
    @RequestMapping("tohtml")
    public String tohtml() {
        return "html/index.html";
    }
    @RequestMapping("getHtml")
    @ResponseBody
    public Map<String,String> getHtml(String url){
        //获取url文章生成文本
        String html = "";
        String reg = "<html>(.*?)</html>";
        String head=GetBody.getSubUtilSimple(html, reg);
        String HTTPHOST="http://yueang2.oss-cn-qingdao.aliyuncs.com/testimg/";
        String newsBody=head;
        Document doc = Jsoup.parse(newsBody);
        Elements pngs = doc.select("img[data-src]");
        System.out.println(pngs);
        for (Element element : pngs) {
            //获取图片地址
            String imgUrl = element.attr("data-src");
            //下载图片到本地
            String filename=DownLoadImg.downloadPicture(imgUrl);
            File file =new File("D:\\m2\\"+filename);
            //上传至oss
            Boolean flag = OssUtil2.uploadFileToOss(file, "testimg/"+filename);
            if(flag) {
                file.delete();
            }
            String newsrc =HTTPHOST + filename;
            element.attr("src", newsrc);
        }
        newsBody = doc.toString();
        System.out.println(newsBody);
        Map<String,String> map=new HashMap<String, String>();
        map.put("resultHtml", newsBody);
        return map;
    }
}